package com.wanmait.recruit.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Admin;
import com.wanmait.recruit.pojo.AdminLog;
import com.wanmait.recruit.pojo.AdminRole;
import com.wanmait.recruit.pojo.Role;
import com.wanmait.recruit.service.AdminLogService;
import com.wanmait.recruit.service.AdminRoleService;
import com.wanmait.recruit.service.RoleService;
import com.wanmait.recruit.service.impl.AdminLogServiceImpl;
import com.wanmait.recruit.service.AdminService;
import com.wanmait.recruit.util.CookieUtil;
import com.wanmait.recruit.util.Md5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;

@Controller
@Transactional
@RequestMapping("/manage")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private AdminLogService adminLogService;
    @Resource
    private RoleService roleService;
    @Resource
    private AdminRoleService adminRoleService;
    @RequestMapping("index")
    public void index(){}
    @GetMapping("/admin/login")
    public void login(HttpServletRequest request){
//        String realIp = request.getHeader("x-forwarded-for");
//        String ip = request.getRemoteAddr();
//        Enumeration en = request.getHeaderNames();
//        while(en.hasMoreElements()) {
//            String name = (String)en.nextElement();
//            String value = request.getHeader(name);
//            System.out.println("name:"+name);
//            System.out.println("value:"+value);
//        }
//        String requestUrlIP = request.getServerName();
//        System.out.println("realIp:"+realIp);
//        System.out.println("ip:"+ip);
//        System.out.println("requestUrlIP"+requestUrlIP);
    }
    @PostMapping("/admin/login")
    public String login(Admin admin, HttpServletRequest request, HttpServletResponse response){
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(),admin.getPassword());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        admin = (Admin)subject.getPrincipal();
        try{
            request.getSession().setAttribute("admin",admin);
            AdminLog adminLog = new AdminLog();
            adminLog.setAdmin(admin);
            adminLog.setLogInfo("login");
            adminLog.setIp(request.getRemoteAddr());
            adminLogService.insert(adminLog);
//            String remember = request.getParameter("remember");
//            if(remember!=null){
//                CookieUtil.addCookie("adminName",admin.getUsername(),"/",30*24*60*60,response);
//                String newPass = Md5Util.md5Super(admin.getPassword());
//                CookieUtil.addCookie("adminPass",newPass,"/",30*24*60*60,response);
//            }
//            else{
//                CookieUtil.addCookie("adminName","","/",0,response);
//                CookieUtil.addCookie("adminPass","","/",0,response);
//            }
            return "redirect:/manage/index";
        }
        catch (Exception e){
            return "redirect:/manage/admin/login";
        }
    }
    @RequestMapping("admin/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();;
        Admin admin =  (Admin)subject.getPrincipal();
        AdminLog adminLog = new AdminLog();
        adminLog.setAdmin(admin);
        adminLog.setLogInfo("logout");
        adminLog.setIp(request.getRemoteAddr());
        adminLogService.insert(adminLog);
        request.getSession().removeAttribute("admin");
//        CookieUtil.addCookie("adminName","","/",0,response);
//        CookieUtil.addCookie("adminPass","","/",0,response);
        return "redirect:/manage/admin/login";
    }
    @RequestMapping("admin/adminList")
    public void adminList(){

    }
    @RequestMapping("admin/adminListData")
    public ModelAndView adminListData(String name,Integer pageNum){
//        if(pageNum==null||pageNum==0){
//            pageNum=1;
//        }
        PageInfo<Admin> pageInfo = adminService.findPage(pageNum,name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("manage/admin/adminListData");
        return modelAndView;
    }
    @RequestMapping("admin/adminRoles")
    public ModelAndView adminRoles(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roles = roleService.findAll();
        Admin admin = adminService.findById(id);
        modelAndView.addObject("admin",admin);
        modelAndView.addObject("roles",roles);
        modelAndView.setViewName("manage/admin/adminRoles");
        return modelAndView;
    }
    @RequestMapping("admin/adminRolesInsert")
    public String adminRolesInsert(Integer adminId,Integer roleId,HttpServletRequest request){
        AdminRole adminRole = new AdminRole();
        adminRole.setAdminId(adminId);
        adminRole.setRoleId(roleId);
        adminRoleService.insert(adminRole);
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        AdminLog adminLog = new AdminLog();
        adminLog.setAdmin(admin);
        adminLog.setLogInfo("给"+adminService.findById(adminId).getUsername()+"添加"+roleService.findById(roleId).getName()+"角色");
        adminLog.setIp(request.getRemoteAddr());
        adminLogService.insert(adminLog);
        return "redirect:/manage/admin/adminRoles";
    }
    @RequestMapping("admin/adminRolesDelete")
    public String adminRolesDelete(Integer adminId,Integer roleId,HttpServletRequest request){
        adminRoleService.deleteById(adminId,roleId);
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        AdminLog adminLog = new AdminLog();
        adminLog.setAdmin(admin);
        adminLog.setLogInfo("给"+adminService.findById(adminId).getUsername()+"删除"+roleService.findById(roleId).getName()+"角色");
        adminLog.setIp(request.getRemoteAddr());
        adminLogService.insert(adminLog);
        return "redirect:/manage/admin/adminRoles";
    }
    @RequestMapping("admin/delete")
    public String delete(Integer id){
        adminService.deleteById(id);
        return "redirect:/manage/admin/adminList";
    }
    @PostMapping("admin/updatePass")
    public String updatePass(Integer id,String pass,HttpServletRequest request,HttpServletResponse response){
        adminService.updatePass(id, pass);
        request.getSession().removeAttribute("admin");
//        CookieUtil.addCookie("adminName","","/",0,response);
//        CookieUtil.addCookie("adminPass","","/",0,response);
        return "redirect:/manage/admin/login";
    }
    @RequestMapping("admin/updatePass")
    public void updatePass(){

    }
    @RequestMapping("admin/isRename")
    @ResponseBody
    public String isRename(String name){
        Boolean flag = adminService.isRename(name);
        if(flag){
            return "1";
        }else{
            return "0";
        }
    }
    @PostMapping("admin/insert")
    public String insert(String name,String pass){
        adminService.insert(name,pass);
        return "redirect:/manage/admin/insert";
    }
    @RequestMapping("admin/insert")
    public void insert(){

    }
}
